package com.face.service;

import com.face.common.Page;
import com.face.dao.RoleDao;
import com.face.entity.Role;
import com.face.exception.RoleExistException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RoleService {

    @Autowired
    private RoleDao roleDao;

    public Page<Role> findPage(String shopId, String name, Integer status, Integer page, Integer limit){
        if (StringUtils.isBlank(name) && status == null) {
            return roleDao.findPage(shopId, page, limit);
        }
        if (StringUtils.isBlank(name)) {
            return roleDao.findPage(shopId, status, page, limit);
        } else if (status == null) {
            return roleDao.findPage(shopId, name, page, limit);
        } else {
            return roleDao.findPage(shopId, name, status, page, limit);
        }
    }

    public Integer save(Role role) {
        Role ExistRole = roleDao.findByName(role.getName(), role.getShopId());
        if (ExistRole != null) {
            throw new RoleExistException("该角色已存在");
        }
        role.setStatus(0);
        role.setCreatetime(new Date());
        role.setUpdatetime(new Date());
        roleDao.save(role);
        return role.getId();
    }

    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    public void update(Role role) {
        role.setUpdatetime(new Date());
        roleDao.update(role);
    }

    public void updateStatus(Integer id, Integer status) {
        roleDao.updateStatus(id, status);
    }
}
